package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.SaleChartQueryObject;
import cn.wolfcode.crm.service.IChartService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
	@Autowired
	private IChartService service;

	@RequestMapping("")
	public String index(){
		return "chart/saleChartList";
	}

	@RequestMapping("sale")
	@ResponseBody
	public List<Map<String,Object>> selectSaleChart(SaleChartQueryObject qo)
	{
		return service.selectSaleChart(qo);
	}


	@RequestMapping("/saleByBar")
	public String saleByBar(Model model, @ModelAttribute("qo")SaleChartQueryObject qo) {

		model.addAttribute("groupByType", SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		List<Map<String, Object>> selectSaleChar = service.selectSaleChart(qo);
		List<Object> groupByTypes = new ArrayList<>();
		List<Object> totalAmounts = new ArrayList<>();
		for (Map<String,Object> map : selectSaleChar) {
			groupByTypes.add(map.get("groupByType"));
			totalAmounts.add(map.get("totalAmount"));
		}
		model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
		model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
		return "/chart/saleByBar";
	}

	@RequestMapping("/saleByPic")
	public String saleByPie(Model model, @ModelAttribute("qo")SaleChartQueryObject qo) {

		model.addAttribute("groupByType", SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		List<Map<String, Object>> selectSaleChar = service.selectSaleChart(qo);
		List<Object> groupByTypes = new ArrayList<>();
		List<Map<String,Object>> datas = new ArrayList<>();
		for (Map<String,Object> map : selectSaleChar) {
			groupByTypes.add(map.get("groupByType"));
			Map<String,Object> data = new HashMap<>();
			data.put("value", map.get("totalAmount"));
			data.put("name", map.get("groupByType"));
			datas.add(data);
		}
		model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
		model.addAttribute("datas", JSON.toJSONString(datas));
		return "/chart/saleByPic";
	}
}























	/*@Autowired
	private IChartService service;
	@Autowired
	private ISupplierService supplierService;
	@Autowired
	private IClientService clientService;
	@Autowired
	private IBrandService brandService;


	@RequiredPermission("销售报表")
	@RequestMapping("sale")
	public String list2(Model model, @ModelAttribute("qo")SaleChartQueryObject qo) {
		model.addAttribute("saleCharts", service.selectSaleChart(qo));
		model.addAttribute("clients", clientService.selectAll());
		model.addAttribute("brands", brandService.selectAll());
		model.addAttribute("groupByTypes",SaleChartQueryObject.groupByTypes );
		return "chart/saleChartList";
	}

	@RequestMapping("saleByBar")
	public String saleByBar(Model model, @ModelAttribute("qo")SaleChartQueryObject qo) {
		model.addAttribute("groupByType", SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		//获取到所有的分组类型
		List<Map<String,Object>> saleChart = service.selectSaleChart(qo);
		List<Object> groupByTypes = new ArrayList<>();
		List<Object> totalAmounts = new ArrayList<>();
		for (Map<String, Object> map : saleChart) {
			groupByTypes.add(map.get("groupByType"));
			totalAmounts.add(map.get("totalAmount"));
		}
		model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
		model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
		return "chart/saleChartByBar";
	}
	@RequestMapping("saleByPie")
	public String saleByPie(Model model, @ModelAttribute("qo")SaleChartQueryObject qo) {
		model.addAttribute("groupByType", SaleChartQueryObject.groupByTypes.get(qo.getGroupByType()));
		//获取到所有的分组类型
		List<Map<String,Object>> saleChart = service.selectSaleChart(qo);
		List<Object> groupByTypes = new ArrayList<>();
		List<Map<String,Object>> datas = new ArrayList<>();
		for (Map<String, Object> map : saleChart) {
			groupByTypes.add(map.get("groupByType"));
			Map<String,Object> map2 = new HashMap<>();
			map2.put("name",map.get("groupByType")) ;
			map2.put("value",map.get("totalAmount")) ;
			datas.add(map2);
		}
		model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
		model.addAttribute("datas", JSON.toJSONString(datas));
		return "chart/saleChartByPie";
	}*/